//
//  KYSimultaneouslyViewController.m
//  kuaiyi
//
//  Created by 杨亮 on 16/3/15.
//  Copyright © 2016年 kuaiyi. All rights reserved.
//

#import "KYSimultaneouslyViewController.h"
#import "KYSimultaneousModel.h"
#import "UIBarButtonItem+Extension.h"


@interface KYSimultaneouslyViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong)NSArray *modelList;

@end

@implementation KYSimultaneouslyViewController
-(NSArray *)modelList{
    if (!_modelList) {
        _modelList = [NSArray array];
    }
    return _modelList;
}
-(NSMutableArray *)selectedModelArray{
    if (!_selectedModelArray) {
        _selectedModelArray = [NSMutableArray array];
    }
    return _selectedModelArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    UITableView *table = [[UITableView alloc]init];
    self.view = table;
    table.dataSource = self;
    table.delegate = self;
    __weak typeof(self) weakSelf = self;
    [KYSimultaneousModel getSimultaneousModelSuccess:^(NSArray *simultaneousModel) {
        weakSelf.modelList = simultaneousModel;
        [table reloadData];
    }];
    [self setUpNav];
}
-(void)setUpNav{
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem setupNoUnderLineNavBarBtnWithTitle:@"确定" andTarget:self action:@selector(okBtnKlick)];
}
-(void)okBtnKlick{
    if ([self.delegate respondsToSelector:@selector(didSelectSimultaneous:simultaneous:)]) {
        [self.delegate didSelectSimultaneous:self simultaneous:self.selectedModelArray];
    }
    [self.navigationController popViewControllerAnimated:YES];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.modelList.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"simultaneousIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    KYSimultaneousModel *model = self.modelList[indexPath.row];
    if ([self.selectedModelArray containsObject:model]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
//        cell.selectionStyle = UITableViewCellSelectionStyleDefault;
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
//    for (KYSimultaneousModel *simModel in self.selectedModelArray) {
//        if (simModel.complication_id == model.complication_id) {
//            cell.accessoryType = UITableViewCellAccessoryCheckmark;
//        }else{
//            cell.accessoryType = UITableViewCellAccessoryNone;
//        }
//    }
    
    cell.textLabel.text = model.complication_name;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    KYSimultaneousModel *model = self.modelList[indexPath.row];
    if ([self.selectedModelArray containsObject:model]) {
        [self.selectedModelArray removeObject:model];
        
    }else{
        [self.selectedModelArray addObject:model];
    }

    [tableView reloadData];
}
@end
